<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="开标时间" prop="open">
        <el-date-picker clearable @clear="handleQuery" v-model="queryParams.open" type="date" value-format="yyyy-MM-dd" @change="handleQuery" placeholder="请选择开标时间"></el-date-picker>
      </el-form-item>
      <!-- <el-form-item>
        <el-button type="warning"  size="mini" @click="queryList('-1')">昨日开标</el-button>
        <el-button type="primary"  size="mini" @click="queryList('0')">今日开标</el-button>
        <el-button type="success"  size="mini" @click="queryList('1')">明日开标</el-button>
      </el-form-item> -->
    </el-form>

    <el-table v-loading="loading" :data="sectionList" border>
      <el-table-column label="序号" type="index"  width="55" align="center">
      </el-table-column>
      <el-table-column label="招标编号" align="center" prop="sectionId"  width="160" show-overflow-tooltip>
      </el-table-column>
        <el-table-column label="招标名称" align="center" prop="sectionName"  width="600" show-overflow-tooltip>
        <template slot-scope="scope">
          <span @dblclick="copyText(scope.row.sectionName)">{{ scope.row.sectionName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="招标类别" align="center"  width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_bidcategory" :value="scope.row.projectInfo.bidcategory"/>
        </template>
      </el-table-column>
      <el-table-column label="组织方式" align="center"  width="180">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_bidmethos" :value="scope.row.bidmethod"/>
        </template>
      </el-table-column>
      <el-table-column label="开标时间" align="center" prop="open" width="140" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.open, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="负责人"  align="center" prop="projectInfo.assistant"  width="120" show-overflow-tooltip >
</el-table-column>
      <el-table-column label="开标记录情况" fixed="right"  align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
          type="text"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd(scope.row)"
          v-hasPermi="['system:todaySection:upload']"
        >上传</el-button>
        <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="handleExport(scope.row)"
            :disabled="!scope.row.openfilePath"
            v-hasPermi="['system:todaySection:export']"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="开标记录" prop="openfilePath">
          <file-upload v-model="form.openfilePath"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import { listSection, todayOpen } from "@/api/projectManage/sectionList"
import { mapGetters } from "vuex"
import { getToken } from "@/utils/auth"
export default {
  name: "Section",
  dicts: ['sys_bidmethos', 'sys_openarea', 'sys_bidway', 'is_switch', 'section_progress', 'sys_bidcategory', 'sys_nego_source', 'sys_enginer', 'section_stop'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 标段列表表格数据
      sectionList: [],
      // 标段详情
      sectionDetail: {
        tabName: 'project',
        open: false,
        title: '标段详情',
        form: {},
        projectInfo: {}
      },
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        open: '',
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {
        id: '',
        openfilePath: ''
      },
      rules: {
        openfilePath: [
          { required: true, message: "开标记录表不能为空", trigger: "change" }
        ]
      },
      // 投标信息相关
      /* 投标信息导入参数 */
      upload: {
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传携带参数
        data: {
          id: null,
          sectionProgress: null
        },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/tender/importData"
      },
      bidTitle: '',
      bidOpen: false
    };
  },
  computed: {
    ...mapGetters(['roles', 'userInfo'])
  },
  created() {
    if (this.roles && (this.roles.indexOf('zb_b') > -1 || this.roles.indexOf('zb_f') > -1)) {
      // 招标部
      this.queryParams.deptId = this.userInfo.deptId
      this.queryParams.bidcategorys = null
    } else if (this.roles.indexOf('fg_gf') > -1) {
      // 分管工程服务
      this.queryParams.bidcategorys = [0, 2]
      this.queryParams.deptId = null
    } else if (this.roles.indexOf('fg_h') > -1) {
      // 分管货物
      this.queryParams.bidcategorys = [1]
      this.queryParams.deptId = null
    } else {
      this.queryParams.deptId = null
      this.queryParams.bidcategorys = null
    }
    this.queryParams.open = this.parseTime(new Date(), '{y}-{m}-{d}')
    this.getList()
  },
  methods: {
    // 表单重置
    reset() {
      this.form = {
        openfilePath: '',
        id: ''
      };
      this.resetForm("form");
    },
      // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
      /** 上传按钮操作 */
    handleAdd(row) {
      this.reset();
      this.open = true;
      this.title = "上传开标记录表"
      this.form.id = row.id
    },
    queryList(flag) {
      let today = new Date();
      this.queryParams.open = new Date()
      if (flag == '1') {
        // 明天
        this.queryParams.open.setDate(today.getDate() + 1)
        this.queryParams.open = this.queryParams.open.toISOString().slice(0, 10)
      } else if (flag == '0') {
        // 今日
        this.queryParams.open = this.queryParams.open.toISOString().slice(0, 10)
      } else if (flag == '-1') {
        // 昨日
        this.queryParams.open.setDate(today.getDate() - 1)
        this.queryParams.open = this.queryParams.open.toISOString().slice(0, 10)
      }
      this.getList()
    },
    /** 查询标段列表 */
    getList() {
      this.loading = true;
      listSection(this.queryParams).then(response => {
        this.sectionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = 10
      this.getList()
    },
    async copyText(textToCopy) {
      // navigator clipboard 需要https等安全上下文
      if (navigator.clipboard && window.isSecureContext) {
        // navigator clipboard 向剪贴板写文本
        navigator.clipboard.writeText(textToCopy).then(() => {
          this.$modal.msgSuccess('复制成功！')
        })
      } else {
        // 创建text area
        let textArea = document.createElement("textarea");
        textArea.value = textToCopy;
        // 使text area不在viewport，同时设置不可见
        textArea.style.position = "absolute";
        textArea.style.opacity = '0';
        textArea.style.left = "-999999px";
        textArea.style.top = "-999999px";
        document.body.appendChild(textArea);
        textArea.focus();
        textArea.select();
        let res = new Promise((res, rej) => {
          res(() => {
            document.execCommand('copy')
            textArea.remove();
          })
          rej(false)
        })
        res.then(res => {
          res();
          this.$modal.msgSuccess('复制成功！')
        }).catch(error => {
          this.$modal.msgError('复制失败！')
        })
      }
    },
    submitForm() {
      console.log(this.form)
      this.$refs["form"].validate(valid => {
        if (valid) {
          todayOpen(this.form).then(response => {
            this.$modal.msgSuccess("今日开标记录提交成功！");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    getFileName(name) {
      // 如果是url那么取最后的名字 如果不是直接返回
      if (name.lastIndexOf("/") > -1) {
        return name.slice(name.lastIndexOf("/") + 1);
      } else {
        return name;
      }
    },
    /** 导出按钮操作 */
    handleExport(row) {
      const link = document.createElement('a');
      link.href = process.env.VUE_APP_BASE_API + row.openfilePath
      link.download = this.getFileName(row.openfilePath)
      link.target = "_blank"; // 可选，如果希望在新窗口中下载文件，请取消注释此行
      link.click()
    }
  }
};
</script>

<style lang="scss" scoped>
::v-deep .el-dialog{
  .el-date-editor, .el-input, .el-select {
    width: 100%;
  }
  .el-pagination {
    .el-date-editor, .el-input, .el-select {
      width: auto;
    }
  }
  .el-dialog__body {
    padding: 30px 10px 30px 30px;
  }
  .el-form-item__label {
    max-width: 100px; /* 你可以根据需要调整这个值 */
    white-space: nowrap; /* 防止文字换行 */
    overflow: hidden; /* 超出部分隐藏 */
    text-overflow: ellipsis; /* 文字溢出时显示省略号 */
  }
  .is-disabled{
    .el-textarea__inner, .el-input__inner {
      color: #333333;
    }
    .el-radio__label {
      color: #333333;
    }
    .el-radio__inner {
      border-color: #333333;
      &::after {
        background: #333333;
      }
    }
  }
  .pirce {
    width: 92%;
  }
}
.bid-dialog-header {
  display: flex;
  > .el-button {
    margin-right: 20px;
  }
}
.bidTable {
  margin-top: 20px;
}
// .popper-class-name {
//   margin: 0 10px 0 10px;
// }
::v-deep .el-popover__reference-wrapper {
  display: inline-block;
  margin: 0 10px 0 10px;
}
.section-detail-title {
  font-size: 16px;
  color: #333333;
  font-weight: bold;
  margin-bottom: 20px;
}

::v-deep .el-tabs {
  .el-tabs__item {
    font-size: 16px;
    font-weight: bold;
    color: #333333;
  }
}
</style>
